package com.jqkb.pages;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.fasterxml.jackson.databind.JsonNode;
import com.jqkb.R;
import com.jqkb.TheApplication;
import com.jqkb.service.ActionService;
import com.jqkb.service.NewMessageService;
import com.jqkb.service.UserService;
import com.jqkb.service.bean.User;
import com.jqkb.service.util.CallBack;
import com.jqkb.service.util.Converter;
import com.jqkb.service.util.SimpleApi;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class MainActivity extends AppCompatActivity {

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnSendAction = (Button) findViewById(R.id.btnSendAction);
        User user = TheApplication.getCurrentUser();
        if (!user.hasPermission("CreateAction")) {
            btnSendAction.setVisibility(View.GONE);
        }


    }

    public void startAction(View view) {
        startActivity(new Intent(this, ActionActivity.class));
    }

    public void startInfoList(View view) {
        startActivity(new Intent(this, PendingForwardActivity.class));
    }

    public void startBackward(View view) {
        startActivity(new Intent(this, PendingBackwardActivity.class));
    }

    public void startProcessed(View view) {
        startActivity(new Intent(this, ForwardListActivity.class));
    }

    public void startBackInfo(View view) {
        startActivity(new Intent(this, BackwardListActivity.class));
    }

    public void startUserInfo(View view) {
        startActivity(new Intent(this, UserInfoActivity.class));
    }

    public void exitLogin(View view) {

        SharedPreferences.Editor edit = TheApplication.getSharedPreferences().edit();
        edit.remove("user");
        edit.apply();
        edit.commit();
        stopService(new Intent(MainActivity.this, NewMessageService.class));
        TheApplication.setCurrentUser(null);
        startActivity(new Intent(MainActivity.this, LoginActivity.class));
        finish();

        /*
        * 为了安全从服务器端也要执行退出
        * */
        final UserService service = SimpleApi.create(UserService.class);
        Call<ResponseBody> call = service.exit();

        Converter.convert(Object.class, call, new CallBack<Object>() {
            @Override
            public void onSuccess(Object object, JsonNode data) {

                if (data.path("code").asInt() == 2000) {


                } else {
                    Toast.makeText(MainActivity.this, data.path("message").asText(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFail(boolean success, int code, String message, JsonNode data) {

                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNetworkFail(Call<ResponseBody> jsonNode) {
                Toast.makeText(MainActivity.this, "网路连接失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Call<ResponseBody> response) {

            }
        });

    }
}
